package com.example.walkingtour;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class WalkingTour extends FragmentActivity {

	public static final int MENU_ADD = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("WalkingTour", "onCreate start");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_walkingtour);
		Log.d("WalkingTour", "onCreate end");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_walkingtour, menu);
		Log.d("WalkingTour", "onCreateOptionsMenu");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_newItem:
			createItem();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void createItem() {
		// Intent intent = new Intent(this, WTItemEdit.class);
		// startActivityForResult(intent, MENU_ADD);
		FragmentManager fm = this.getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		Fragment insertFragment = (Fragment) fm.findFragmentByTag("add");

		if (insertFragment != null) {
			ft.remove(insertFragment).addToBackStack("");
		}
		Fragment f = new WTItemEdit();
		Bundle b = new Bundle();
		b.putInt("ACTION", MENU_ADD);
		f.setArguments(new Bundle());
		ft.add(R.id.edit_fragment, f, "add").addToBackStack("").commit();
	}
}
